package com.liumuxian.exam.web.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liumuxian.exam.dao.ExamPerformanceDataDao;
import com.liumuxian.exam.data.ExamPerformanceVO;
import com.liumuxian.exam.pojo.Article;
import com.liumuxian.exam.pojo.ExamPerformance;
import com.liumuxian.exam.pojo.ExamPerformanceData;
import com.liumuxian.exam.service.ExamPerformanceDataService;
import com.liumuxian.exam.service.ExamPerformanceService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/examperformance")
public class ExamPerformanceController {
    @Autowired
    private ExamPerformanceService examPerformanceService;
    @Autowired
    private ExamPerformanceDataService examPerformanceDataService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/save")
    public Result save(@RequestBody ExamPerformance examPerformance){
        examPerformanceService.save(examPerformance);
        return new Result(true, StatusCode.OK,"保存成功！");
    }
    @PostMapping("/saveDate")
    public Result saveDate(@RequestBody String epid,@RequestBody ExamPerformanceVO examPerformance){
        Map<String,Object> examPerformanceVO = (Map<String,Object>) redisTemplate.opsForValue().get("ExamPerformance_"+epid);
        if(examPerformance==null){
            Map<String,Object> map = new HashMap<>();
            map.put(examPerformance.getQid(),examPerformance);
            Claims claims= (Claims) request.getAttribute("user_claims");
            if(claims==null){
                throw new RuntimeException("请先登录！");
            }
            redisTemplate.opsForValue().set("ExamPerformance_"+claims.getId(),map);
            return new Result(true, StatusCode.OK,"保存成功！");
        }else {
            examPerformanceVO.put(examPerformance.getQid(),examPerformance);
            return new Result(true, StatusCode.OK,"保存成功！");
        }
    }
    @PostMapping("/submit")
    public Result saveDateflist(@RequestBody Map map){
        String epid =(String) map.get("epid");
        List<ExamPerformanceVO> examPerformance =(List<ExamPerformanceVO>) map.get("examPerformance");
        ObjectMapper mapper = new ObjectMapper();
        List<ExamPerformanceVO> list = mapper.convertValue(examPerformance, new TypeReference<List<ExamPerformanceVO>>() { });
        examPerformanceService.submit(epid,list);
        Claims claims= (Claims) request.getAttribute("user_claims");
        if(claims==null){
            throw new RuntimeException("请先登录！");
        }
        redisTemplate.delete("ExamPerformance_"+claims.getId());

        return new Result(true, StatusCode.OK,"保存成功！");
    }
    @GetMapping("/user/list")
    public Result list(){
        Map<String,String> map = new HashMap<>();
        Claims claims= (Claims) request.getAttribute("user_claims");
        if(claims==null){
            throw new RuntimeException("请先登录！");
        }
        map.put("userid",claims.getId());
        map.put("state","1");
        List examPerformances = examPerformanceService.findAll(map);
        return new Result(true, StatusCode.OK,"保存成功！",examPerformances);
    }
    @GetMapping("/view/{id}")
    public Result view(@PathVariable String id){
        ExamPerformance examPerformances = examPerformanceService.findByIid(id);
        return new Result(true, StatusCode.OK,"查询成功！",examPerformances);
    }
    @GetMapping("/views/{epid}/{qid}")
    public Result views(@PathVariable String epid,@PathVariable String qid){
        ExamPerformanceData examPerformanceData = examPerformanceDataService.findByExamperformanceidAndQuestionid(epid,qid);
        return new Result(true, StatusCode.OK,"查询成功！",examPerformanceData);
    }
    @PostMapping("/update")
    public Result update(@RequestBody ExamPerformance examPerformance){
        examPerformanceService.update(examPerformance);
        return new Result(true, StatusCode.OK,"修改成功！");
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        examPerformanceService.delete(id);
        return new Result(true, StatusCode.OK,"删除成功！");
    }
    @GetMapping("/list")
    public Result findAllGesTate(){
        List<ExamPerformance> examPerformanceList = examPerformanceService.findAllGesTate(null,0);
        return new Result(true, StatusCode.OK,"查询成功！",examPerformanceList);
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        ExamPerformance examPerformanceList = examPerformanceService.findById(id);
        return new Result(true, StatusCode.OK,"查询成功！",examPerformanceList);
    }


}
